<html>
<head><meta charset="utf-8"><title>Why can&#x27;t I wrap this data structure to make it Sync + Send? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20I.20wrap.20this.20data.20structure.20to.20make.20it.20Sync.20.2B.20Send.3F.html">Why can&#x27;t I wrap this data structure to make it Sync + Send?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221559307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20I%20wrap%20this%20data%20structure%20to%20make%20it%20Sync%20%2B%20Send%3F/near/221559307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Woolcock <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20I.20wrap.20this.20data.20structure.20to.20make.20it.20Sync.20.2B.20Send.3F.html#221559307">(Jan 04 2021 at 18:12)</a>:</h4>
<p>I'm working with html5ever (or rather, a library based on it) and I'm having a problem because I'm trying to use a data structure containing a <code>StrTendril</code> in a <code>Future</code>. rustc complains, understandably, that the value is not Sync or Send because Tendril contains a <code>Cell&lt;usize&gt;</code>. Usually I could just wrap the data structure in an <code>Arc&lt;Mutex&lt;&gt;&gt;</code> and be done with it, but that doesn't work in this case, I still get the error about <code>Cell&lt;usize&gt;</code> not being Sync. Here's a minimal example:</p>
<div class="codehilite"><pre><span></span><code>use std::sync::{Mutex, Arc};
use html5ever::tendril::{Tendril, StrTendril, fmt::UTF8, NonAtomic, Atomic};

fn foo&lt;D: Send&gt;(d: D) { }

fn main() {
    let tendril = Tendril::&lt;UTF8, NonAtomic&gt;::new();
    let tendril = Arc::new(Mutex::new(tendril));
    /*
    Compiling testproj v0.1.0 (/Users/pwoolcock/Code/testproj)
    error[E0277]: `Cell&lt;usize&gt;` cannot be shared between threads safely
     --&gt; src/main.rs:9:5
      |
    4 | fn foo&lt;D: Send&gt;(d: D) { }
      |           ---- required by this bound in `foo`
    ...
    9 |     foo(tendril);
      |     ^^^ `Cell&lt;usize&gt;` cannot be shared between threads safely
      |
      = help: within `NonAtomic`, the trait `Sync` is not implemented for `Cell&lt;usize&gt;`
      = note: required because it appears within the type `NonAtomic`
      = note: required because of the requirements on the impl of `Send` for `Tendril&lt;UTF8&gt;`
      = note: required because of the requirements on the impl of `Send` for `Mutex&lt;Tendril&lt;UTF8&gt;&gt;`
      = note: required because of the requirements on the impl of `Send` for `Arc&lt;Mutex&lt;Tendril&lt;UTF8&gt;&gt;&gt;`

    error: aborting due to previous error; 1 warning emitted

    For more information about this error, try `rustc --explain E0277`.
    error: could not compile `testproj`

    To learn more, run the command again with --verbose.
    */
    foo(tendril);
}
</code></pre></div>
<p>I could use <code>Tendril&lt;UTF8, Atomic&gt;</code>, but that is not allowed in the <code>TreeSink</code> trait so I'm stuck. Is there any way to wrap this to allow it to be Send + Sync?</p>



<a name="221560131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20I%20wrap%20this%20data%20structure%20to%20make%20it%20Sync%20%2B%20Send%3F/near/221560131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20I.20wrap.20this.20data.20structure.20to.20make.20it.20Sync.20.2B.20Send.3F.html#221560131">(Jan 04 2021 at 18:18)</a>:</h4>
<p><code>&amp;T</code> is send when <code>T</code> is sync. If <code>Cell&lt;usize&gt;</code> was sync, <code>&amp;Cell&lt;usize&gt;</code> would be send, which makes it trivial to get a data race.</p>



<a name="221560330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20I%20wrap%20this%20data%20structure%20to%20make%20it%20Sync%20%2B%20Send%3F/near/221560330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Woolcock <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20I.20wrap.20this.20data.20structure.20to.20make.20it.20Sync.20.2B.20Send.3F.html#221560330">(Jan 04 2021 at 18:20)</a>:</h4>
<p>Sure, but shouldn't anything protected by a <code>Mutex</code> be <code>Sync</code>?</p>



<a name="221561183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20I%20wrap%20this%20data%20structure%20to%20make%20it%20Sync%20%2B%20Send%3F/near/221561183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20I.20wrap.20this.20data.20structure.20to.20make.20it.20Sync.20.2B.20Send.3F.html#221561183">(Jan 04 2021 at 18:27)</a>:</h4>
<p><code>Tendril&lt;F, A&gt;</code> requires <code>A: Sync</code> for it to be <code>Send</code>: <a href="https://docs.rs/html5ever/0.25.1/html5ever/tendril/struct.Tendril.html">https://docs.rs/html5ever/0.25.1/html5ever/tendril/struct.Tendril.html</a></p>



<a name="221561361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20I%20wrap%20this%20data%20structure%20to%20make%20it%20Sync%20%2B%20Send%3F/near/221561361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Woolcock <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20I.20wrap.20this.20data.20structure.20to.20make.20it.20Sync.20.2B.20Send.3F.html#221561361">(Jan 04 2021 at 18:28)</a>:</h4>
<p>yes, but as I stated above, as soon as I change <code>A</code> to <code>Atomic</code>, I can no longer implement the <code>TreeSink</code> trait, which I also need</p>



<a name="221562604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20I%20wrap%20this%20data%20structure%20to%20make%20it%20Sync%20%2B%20Send%3F/near/221562604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20I.20wrap.20this.20data.20structure.20to.20make.20it.20Sync.20.2B.20Send.3F.html#221562604">(Jan 04 2021 at 18:38)</a>:</h4>
<p>I can't help with this. Try asking one of the authors of html5ever.</p>



<a name="221564351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20I%20wrap%20this%20data%20structure%20to%20make%20it%20Sync%20%2B%20Send%3F/near/221564351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20I.20wrap.20this.20data.20structure.20to.20make.20it.20Sync.20.2B.20Send.3F.html#221564351">(Jan 04 2021 at 18:52)</a>:</h4>
<p>After looking a bit at the design of the crate, it seems to me that, either the <code>TreeSink</code> trait itself ought to be atomic over the <code>Atomicity</code>, or that its methods should. So what you can do is fork the crate, implement those changes, submit them with a PR; and until your PR or an equivalent workaround is published, use your own fork as a "patch" of the official crate (in your <code>Cargo.toml</code>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>